package com.homestay.controller;

import com.homestay.beans.Vip;
import com.homestay.exception.UsedException;
import com.homestay.service.VipService;
import com.homestay.service.impl.VipServiceImpl;
import com.homestay.utils.FormBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/VipServlet.do")
public class VipServlet extends HttpServlet {

    VipService vipService = new VipServiceImpl();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String action = req.getParameter("action");
        switch (action) {
            case "list":
                vip_list(req, resp);
                break;
            case "add":
                vip_add(req, resp);
                break;
            case "delete":
                vip_delete(req, resp);
                break;
            case "get_update":
                vip_get_update(req, resp);
                break;
            case "update":
                vip_update(req, resp);
                break;
            default:
                resp.getWriter().write("<script>alert('未知操作');location.href='views/vip_list.jsp';</script>");
        }
    }


    private void vip_list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        if (phone == null || phone.isEmpty()) {
            phone = "";
        }
        List<Vip> vipList = vipService.queryVipList(phone);
        int count = vipList.size();
        req.setAttribute("list", vipList);
        req.setAttribute("phone", phone);
        req.setAttribute("msg", count > 0 ? "" : "未查询到相关会员信息");
        req.getRequestDispatcher("/views/vip_list.jsp").forward(req, resp);
    }

    private void vip_add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Vip vip = FormBeanUtils.populate(req, Vip.class);
        req.setAttribute("vip", vip);
        try {
            int count = vipService.addVip(vip);
            if (count > 0) {
                resp.getWriter().write("<script>alert('新增会员成功');location.href='views/vip_add.jsp';</script>");
            } else {
//                resp.getWriter().write("<script>alert('新增会员失败');</script>");
                req.setAttribute("msg", "新增会员失败，请检查输入信息");
                req.getRequestDispatcher("/views/vip_add.jsp").forward(req, resp);
            }
        } catch (UsedException e) {
            String message = e.getMessage();
//            resp.getWriter().write("<script>alert('" + message + "');</script>");
            System.out.println("会员信息已存在: " + message);
            req.setAttribute("msg", message);
            req.getRequestDispatcher("/views/vip_add.jsp").forward(req, resp);
        }
    }

    private void vip_delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");

            int count = vipService.deleteVip(id);
            if (count > 0) {
                resp.getWriter().write("<script>alert('删除会员成功');location.href='/VipServlet.do?action=list';</script>");
            } else {
                resp.getWriter().write("<script>alert('删除会员失败');location.href='/VipServlet.do?action=list';</script>");
            }
        } catch (UsedException e) {
            String message = e.getMessage();
            resp.getWriter().write("<script>alert('"+message+"');location.href='/VipServlet.do?action=list';</script>");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void vip_get_update(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Vip vip = vipService.queryVipById(id);
        if (vip != null) {
            req.setAttribute("vip", vip);
            try {
                req.getRequestDispatcher("/views/vip_update.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                resp.getWriter().write("<script>alert('未找到该会员信息');location.href='/VipServlet.do?action=list';</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void vip_update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Vip vip = FormBeanUtils.populate(req, Vip.class);
        req.setAttribute("vip", vip);
        try {
            int count = vipService.updateVip(vip);
            if (count > 0) {
                resp.getWriter().write("<script>alert('更新会员信息成功');location.href='/VipServlet.do?action=list';</script>");
            } else {
                req.setAttribute("msg", "更新会员信息失败，请检查输入信息");
                req.getRequestDispatcher("/views/vip_update.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
